package com.linfeng.component.mybatisplus.tools;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.Optional;

public class PageTools {

    public static <D, V> IPage<V> convertPage(IPage<D> dPage, Class<V> vClass) {

        return Optional.ofNullable(dPage)
                .map(p -> {
                    IPage<V> vPage = new Page<>();
                    BeanUtil.copyProperties(p, vPage);

                    vPage.setRecords(BeanUtil.copyToList(dPage.getRecords(), vClass));

                    return vPage;
                }).orElse(new Page<>());
    }
}
